package com.elitecrm.webchat.robot;

import com.elitecrm.webchat.model.Message;
import com.elitecrm.webchat.model.Session;
import com.elitecrm.webchat.model.User;
import com.elitecrm.webchat.service.http.PoolingHttpService;
import com.elitecrm.webchat.util.StringUtil;
import com.google.common.base.Joiner;
import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class IWenCaiRobot extends RobotEngine {
    private String sessionCode;
    private String qaDataOrigin;
    private String source;
    private String userName;
    private String answerChannel;
    private String userId;
    private String mode;
    private String reqid;
    private String logid;
    private String Brand;
    private long sessionID;
    private int queue_ID;

    public IWenCaiRobot() {
    }

    public IWenCaiRobot(JSONObject recJSON, Session session) {
        if (recJSON.has("name")) {
            this.robotEnginName = recJSON.getString("name");
        } else {
            this.robotEnginName = "问财";
        }

        String url = recJSON.getString("serverUrl");
        this.id = "BOT022";
        this.robotEnginServerUrl = url;
        this.type = 22;
        User client = session.getClient();
        this.sessionID = session.getId();
        this.Brand = client.getBrand();
        this.queue_ID = session.getQueueId();
        this.sessionCode = UUID.randomUUID().toString();
        this.logid = UUID.randomUUID().toString();
        this.reqid = UUID.randomUUID().toString();
        this.userName = client.getName();
        this.userId = client.getId();
        JSONObject propertyJSON = recJSON.getJSONObject("property");
        this.qaDataOrigin = propertyJSON.optString("qaDataOrigin", "commonRobot");
        this.source = propertyJSON.optString("source", "b_zhengquan_huasheng");
        this.version = propertyJSON.optString("version", "1.9");
        this.answerChannel = propertyJSON.optString("answerChannel", "web_other");
        this.mode = propertyJSON.optString("mode", "preCloud");
    }

    public String search(Message msg, long sessionId) {
        List<String> params = new ArrayList();
        try {
            if (StringUtil.isNotNull(msg.getSearchKey())) {
                if (!msg.getSearchKey().equals("input")) {
                    params.add("question=" + URLEncoder.encode(msg.getSearchKey(), "UTF-8"));
                } else {

                    params.add("question=" + URLEncoder.encode(msg.getMessage(), "UTF-8"));
                }
            } else {

                params.add("question=" + URLEncoder.encode(msg.getMessage(), "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            if (StringUtil.isNotNull(msg.getSearchKey())) {
                if (!msg.getSearchKey().equals("input")) {
                    params.add("question=" + msg.getSearchKey());
                } else {

                    params.add("question=" + msg.getMessage());
                }
            } else {

                params.add("question=" + msg.getMessage());
            }
            // params.add("question=" + (StringUtil.isNotNull(msg.getSearchKey()) ? msg.getSearchKey() : msg.getMessage()));
        }
        params.add("session_code=" + this.sessionCode);
        params.add("qaDataOrigin=" + this.qaDataOrigin);
        params.add("source=" + this.source);
        try {
            params.add("user_name=" + URLEncoder.encode(this.userName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            params.add("user_name=" + this.userName);
        }
        params.add("version=" + this.version);
        params.add("user_id=" + this.userId);
        params.add("answerChannel=" + this.answerChannel);
        params.add("mode=" + this.mode);
        params.add("logid=" + this.logid);
        params.add("reqid=" + this.reqid);
        params.add("queue_ID=" + this.queue_ID);
        params.add("sessionID=" + this.sessionID);

        try {
            params.add("Brand=" + URLEncoder.encode(this.Brand, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String url = this.robotEnginServerUrl + "?" + Joiner.on("&").join(params);
        return PoolingHttpService.getInstance().get(url);
    }

    public String feedback(String feedback) {
        if (!StringUtil.isNotNull(feedback)) {
            return "";
        } else {
            JSONObject fb = JSONObject.fromObject(feedback);
            List<String> params = new ArrayList();
            params.add("answerChannel=" + this.answerChannel);
            params.add("companySource=" + this.source);
            params.add("session_code=" + this.sessionCode);
            params.add("qaDataOrigin=" + this.qaDataOrigin);
            params.add("user_id=" + this.userId);
            Iterator var4 = fb.entrySet().iterator();

            while (var4.hasNext()) {
                Object o = var4.next();
                Map.Entry<String, Object> map = (Map.Entry) o;
                params.add((String) map.getKey() + "=" + map.getValue());
            }

            String url = this.robotEnginServerUrl + "/buried/?" + Joiner.on("&").join(params);
            return PoolingHttpService.getInstance().get(url);
        }
    }
}
